package com.example.orderfoodsystem22201310liuziming.controller;

import com.example.orderfoodsystem22201310liuziming.entity.allorder;
import com.example.orderfoodsystem22201310liuziming.entity.foodmenu;
import com.example.orderfoodsystem22201310liuziming.entity.shoppingorder;
import com.example.orderfoodsystem22201310liuziming.repository.AllOrderRepository;
import com.example.orderfoodsystem22201310liuziming.repository.MenuRepository;
import com.example.orderfoodsystem22201310liuziming.repository.OrderRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Controller
@RequestMapping("/seller")
public class SellerController {
    @Autowired
    private MenuRepository menuRepository;
    @Autowired
    private HttpSession httpSession;
    @Autowired
    private OrderRepository orderRepository;
    @Autowired
    private AllOrderRepository allOrderRepository;
    @Value("${dishphoto.url}")
    private String dishUrl;

    @RequestMapping("/") //卖家主页
    protected String sellerindex(){
        return "seller/SellerIndex";
    }

    @RequestMapping("/menulist1") //卖家菜单1
    protected String menuList(String username){
            return "seller/SellerMenuList";
    }

    @RequestMapping("/menulist2")       //卖家菜单2
    protected String indexPage(Model model, @RequestParam(value = "user", required = false, defaultValue = "管理员") String user) {
        List<foodmenu> menuList = new ArrayList<>();
        for (Iterator<foodmenu> it = menuRepository.findAll().iterator(); it.hasNext(); ) {
            menuList.add(it.next());
        }
        model.addAttribute("dishurl",dishUrl);
        model.addAttribute("menuHtml", menuList);
        model.addAttribute("username", user);

        return "seller/SellerMenuList2";
    }

    @RequestMapping("/order2") //卖家总订单表
    protected String order2(Model model){
        Sort sort = Sort.by("time").descending();//排序
        List<allorder> allorderList = new ArrayList<>();
        for (Iterator<allorder> it = allOrderRepository.findAll(sort).iterator(); it.hasNext();)
        {
            allorderList.add(it.next());
        }
        model.addAttribute("buyerHtml", allorderList);

        return "seller/SellerOrder2";
    }

    @RequestMapping("/order/{username}") //卖家具体订单表 根据用户名查询
    protected String order(Model model,@PathVariable("username")String username){
        List<shoppingorder> shoppingorderList = new ArrayList<>();
        for (Iterator<shoppingorder> it = orderRepository.findAllByUsername(username).iterator(); it.hasNext();)
        {
            shoppingorderList.add(it.next());
        }
        model.addAttribute("orderHtml", shoppingorderList);

        return "seller/SellerOrder";
    }

    @RequestMapping("/order2/{id}") //卖家具体订单表 根据id查询
    protected String order(Model model,@PathVariable("id")int id){
        allorder order= allOrderRepository.findById(id);
        String username = order.getUsername();
        List<shoppingorder> shoppingorderList1 = new ArrayList<>();
        for (Iterator<shoppingorder> it = orderRepository.findAllByUsername(username).iterator(); it.hasNext();)
        {
            shoppingorderList1.add(it.next());
        }
        model.addAttribute("orderHtml", shoppingorderList1);

        return "seller/SellerOrder";
    }

//----------------------------------------------------------------------------------------------------------------------

    @RequestMapping("/menuHtml")  //通过html模板
    @ResponseBody
    protected Iterable<foodmenu> test(){
        return menuRepository.findAll();
    }

    @RequestMapping(value = "/menuListJson")
    @ResponseBody
    protected Iterable<foodmenu> menuListJson(){
        return menuRepository.findAll();
    }
}
